package site.marqstree.latte.core.delegates.bottom;

import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.ColorInt;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.joanzapata.iconify.widget.IconTextView;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.Map;

import butterknife.BindView;
import me.yokeyword.fragmentation.ISupportFragment;
import site.marqstree.latte.core.R;
import site.marqstree.latte.core.R2;
import site.marqstree.latte.core.delegates.LatteDelegate;

/*
 * 项目名: EleCommerce
 * 包名: site.marqstree.latte.core.delegates.bottom
 * 文件名: BaseBottomDelegate
 * 创建者: marqstree
 * 创建时间: 2020/1/21 10:03
 * 描述: TODO
 */
public abstract class BaseBottomDelegate extends LatteDelegate implements View.OnClickListener {
    //Tab页导航按钮的图标、字体数组
    private final ArrayList<BottomTabBean> TAB_BEANS = new ArrayList<>();
    //Tab页的Fragment数组
    private final ArrayList<BottomItemDelegate> ITEM_DELEGATES = new ArrayList<>();
    //导航按钮、Tab页的映射Map
    private final LinkedHashMap<BottomTabBean, BottomItemDelegate> ITEMS = new LinkedHashMap<>();
    //当前选中的Tab页的索引
    private int mCurrentDelegate = 0;
    //默认显示的Tab页的索引
    private int mIndexDelegate = 0;
    //选中的Tab页导航按钮的图标、字体颜色
    private int mClickedColor = Color.RED;

    //底部导航条
    @BindView(R2.id.bottom_bar)
    LinearLayout mBottomBar = null;

    //设置内容Fragment
    public abstract LinkedHashMap<BottomTabBean, BottomItemDelegate> setItems(ItemBuilder builder);

    //设置默认内容页索引号
    public abstract int setIndexDelegate();

    //设置底部导航高亮色
    @ColorInt
    public abstract int setClickedColor();

    //设置布局
    @Override
    public Object setLayout() {
        return R.layout.delegate_bottom;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //初始化默认Tab索引号
        mIndexDelegate = setIndexDelegate();
        //初始化选中颜色
        if (setClickedColor() != 0) {
            mClickedColor = setClickedColor();
        }

        //从ec模块获取底部导航按钮数据
        final ItemBuilder builder = ItemBuilder.builder();
        final LinkedHashMap<BottomTabBean, BottomItemDelegate> items = setItems(builder);
        ITEMS.putAll(items);
        for (Map.Entry<BottomTabBean, BottomItemDelegate> item : ITEMS.entrySet()) {
            final BottomTabBean key = item.getKey();
            final BottomItemDelegate value = item.getValue();
            TAB_BEANS.add(key);
            ITEM_DELEGATES.add(value);
        }
    }

    //创建并显示导航tab按钮容器、内容fragment
    @Override
    public void onBindView(@Nullable Bundle savedInstanceState, @NonNull View rootView) {
        //1)创建多个导航tab按钮容器
        final int size = ITEMS.size();
        for (int i = 0; i < size; i++) {
            // 创建bottom_item_icon_text_layout布局对应的View(RelativeLayout)
            // 并作为mBottomBar(LinearLayout)的子view
            //inflate: 根据布局文件创建View
            // 第一个参数：待创建的布局文件的ID
            // 第二个参数：父View。创建的新View作为父ViewGroup的子View
            LayoutInflater.from(getContext()).
                    inflate(R.layout.bottom_item_icon_text_layout, mBottomBar);
            //获取第i个Tab导航按钮容器
            final RelativeLayout item = (RelativeLayout) mBottomBar.getChildAt(i);
            //设置每个tab导航按钮容器的点击事件
            //记录每个tab导航按钮容器的索引号
            item.setTag(i);
            //设置Tab页导航按钮点击事件
            item.setOnClickListener(this);
            //获取每个tab导航按钮容器中的图标、标题控件及绑定数据对象
            final IconTextView itemIcon = (IconTextView) item.getChildAt(0);
            final TextView itemTitle = (TextView) item.getChildAt(1);
            final BottomTabBean bean = TAB_BEANS.get(i);
            //初始化每个tab导航按钮容器中的图标、标题
            itemIcon.setText(bean.getIcon());
            itemTitle.setText(bean.getTitle());
            //设置选中tab导航按钮容器的图标、字体
            if (i == mIndexDelegate) {
                itemIcon.setTextColor(mClickedColor);
                itemTitle.setTextColor(mClickedColor);
            }
        }

        //2)创建多个tab内容fragment
        //一次性加载ITEM_DELEGATES中的所有Fragment，并将mIndexDelegate指定的Fragment在容器中显示
        // loadMultipleRootFragment的作用：
        // 一次性加载多个Fragments，但是containerId只默认显示其中一个,
        //第一个参数：指定容器ID
        //第二个参数：指定显示第几个Fragment
        //第三个参数：待加载的多个Fragment
        // 显示的那个Fragment会依次调用onCreateView()，onLazyInitView()
        // 其他被隐藏的Fragment会依次调用onHiddenChanged(true)，onCreateView()
        //将ArrayList<BottomItemDelegate>转为ISupportFragment[]
        final ISupportFragment[] delegateArray = ITEM_DELEGATES.toArray(new ISupportFragment[size]);
        getSupportDelegate().loadMultipleRootFragment(R.id.bottom_bar_delegate_container, mIndexDelegate, delegateArray);
    }

    //重置Tab导航按钮的图标、文字颜色(灰色)
    private void resetColor() {
        final int count = mBottomBar.getChildCount();
        for (int i = 0; i < count; i++) {
            final RelativeLayout item = (RelativeLayout) mBottomBar.getChildAt(i);
            final IconTextView itemIcon = (IconTextView) item.getChildAt(0);
            itemIcon.setTextColor(Color.GRAY);
            final TextView itemTitle = (TextView) item.getChildAt(1);
            itemTitle.setTextColor(Color.GRAY);
        }
    }

    //点击Tab导航按钮容器(RelativeLayout)
    //显示对应内容Fragment
    //高亮对应Tab导航按钮容器
    @Override
    public void onClick(View v) {
        //从Tab导航的RelativeLayout的Tag中获取索引号
        final int tag = (int) v.getTag();
        //重置所有tab导航按钮的图标、字体为灰色
        resetColor();
        //设置点击Tab导航容器中的字体、图标为选中的颜色
        final RelativeLayout item = (RelativeLayout) v;
        final IconTextView itemIcon = (IconTextView) item.getChildAt(0);
        itemIcon.setTextColor(mClickedColor);
        final TextView itemTitle = (TextView) item.getChildAt(1);
        itemTitle.setTextColor(mClickedColor);
        //显示点击的导航容器
        //showHideFragment：显/隐指定fragment
        //第一个参数：显示选中fragment
        //第二个参数：隐藏上一次选中的fragment
        getSupportDelegate().showHideFragment(ITEM_DELEGATES.get(tag), ITEM_DELEGATES.get(mCurrentDelegate));
        //注意先后顺序
        mCurrentDelegate = tag;
    }
}
